package com.guan.ordering_sys.generator.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.ordering_sys.common.StateCode;
import com.guan.ordering_sys.exception.BaseException;
import com.guan.ordering_sys.generator.entity.CompanyGoods;
import com.guan.ordering_sys.generator.mapper.CompanyGoodsMapper;
import com.guan.ordering_sys.generator.service.CompanyGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  CompanyGoodsServiceImpl 服务实现类
 * </p>
 *
 * @author 关远键
 * @since 2021-06-18
 */
@Service
public class CompanyGoodsServiceImpl extends ServiceImpl<CompanyGoodsMapper, CompanyGoods> implements CompanyGoodsService {

    @Autowired
    private CompanyGoodsMapper companyGoodsMapper;

    /**
     * 通过商品编号获取公司编号
     * @author 关远键
     * @date 2021/6/27 10:58
     * @param id
     * @return java.lang.Long
     */
    @Override
    public Long getByGoodsId(Long id) {
        Long companyId = companyGoodsMapper.getByGoodsId(id);

        if (companyId == null) {
            throw new BaseException(StateCode.NOT_FOUND);
        }

        return companyId;
    }

    /**
     * 公司与商品建立关联
     * @author 关远键
     * @date 2021/6/27 15:09
     * @param companyId 公司编号
     * @param goodsId 商品编号
     * @return int
     */
    @Override
    public int insert(Integer companyId, Long goodsId) {
        CompanyGoods companyGoods = new CompanyGoods();
        companyGoods.setCompanyId(companyId);
        companyGoods.setGoodsId(goodsId);

        if (companyGoodsMapper.insert(companyGoods) > 0) {
            return 1;
        } else {
            throw new BaseException(StateCode.DAO_EXCEPTION);
        }
    }

    /**
     * 删除关联
     * @author 关远键
     * @date 2021/6/27 15:43
     * @param goodsId
     * @return int
     */
    @Override
    public int deleteById(Long goodsId) {
        return companyGoodsMapper.deleteById(goodsId);
    }

}
